package com.example.graduation.controller;

import com.example.graduation.entity.JobApplication;
import com.example.graduation.service.JobApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/job-applications")
public class JobApplicationController {
    @Autowired
    private JobApplicationService jobApplicationService;

    @GetMapping
    public ResponseEntity<List<JobApplication>> getAllApplications() {
        List<JobApplication> applications = jobApplicationService.findAll();
        return ResponseEntity.ok(applications);
    }

    @GetMapping("/{id}")
    public ResponseEntity<JobApplication> getApplicationById(@PathVariable Integer id) {
        JobApplication application = jobApplicationService.findById(id);
        if (application != null) {
            return ResponseEntity.ok(application);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/job-seeker/{jobSeekerId}")
    public ResponseEntity<List<JobApplication>> getApplicationsByJobSeeker(@PathVariable Integer jobSeekerId) {
        List<JobApplication> applications = jobApplicationService.findByJobSeekerId(jobSeekerId);
        return ResponseEntity.ok(applications);
    }

    @GetMapping("/company-job/{companyJobId}")
    public ResponseEntity<List<JobApplication>> getApplicationsByCompanyJob(@PathVariable Integer companyJobId) {
        List<JobApplication> applications = jobApplicationService.findByCompanyJobId(companyJobId);
        return ResponseEntity.ok(applications);
    }

    @GetMapping("/status/{status}")
    public ResponseEntity<List<JobApplication>> getApplicationsByStatus(@PathVariable String status) {
        List<JobApplication> applications = jobApplicationService.findByStatus(status);
        return ResponseEntity.ok(applications);
    }

    @PostMapping
    public ResponseEntity<JobApplication> createApplication(@RequestBody JobApplication jobApplication) {
        int result = jobApplicationService.insert(jobApplication);
        if (result > 0) {
            return ResponseEntity.ok(jobApplication);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<JobApplication> updateApplication(@PathVariable Integer id, @RequestBody JobApplication jobApplication) {
        jobApplication.setId(id);
        int result = jobApplicationService.update(jobApplication);
        if (result > 0) {
            return ResponseEntity.ok(jobApplication);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PutMapping("/{id}/status")
    public ResponseEntity<Void> updateApplicationStatus(@PathVariable Integer id, @RequestParam String status) {
        int result = jobApplicationService.updateStatus(id, status);
        if (result > 0) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteApplication(@PathVariable Integer id) {
        int result = jobApplicationService.deleteById(id);
        if (result > 0) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
} 